﻿Imports System.ComponentModel

Friend MustInherit Class ModelBase
    Implements IModelBase

#Region "Events"
    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
#End Region

#Region "Properties"
#Region "Constants"
    ' Insert Constant Declarations Here
#End Region

#End Region

#Region "Constructors"
    Public Sub New()
    End Sub
#End Region

#Region "Methods"
    ' Insert Method (Function/Sub) Declarations Here

#Region "Event Raiser Methods"
    Protected Sub OnPropertyChanged(e As PropertyChangedEventArgs)
        RaiseEvent PropertyChanged(sender:=Me, e:=e)

        For Each l_propertyName In PropertyDependencyAttribute.GetDependentPropertyNames(Me.GetType(), e.PropertyName)
            If Not String.Equals(l_propertyName, e.PropertyName) Then
                OnPropertyChanged(l_propertyName)
            End If
        Next
    End Sub

    Protected Sub OnPropertyChanged(propertyName As String)
        OnPropertyChanged(New PropertyChangedEventArgs(propertyName))
    End Sub

    ''' <summary>
    ''' When called from within a property's setter or getter, raises the <see cref="PropertyChanged"/> event for that
    ''' property.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Sub OnPropertyChanged()
        Dim l_stack = New StackTrace(skipFrames:=1, fNeedFileInfo:=False)
        Dim l_frame = l_stack.GetFrame(0)
        Dim l_methodName = l_frame.GetMethod().Name
        If l_methodName.StartsWith("set_") OrElse l_methodName.StartsWith("get_") Then
            OnPropertyChanged(l_methodName.Substring(4))
        End If
    End Sub
#End Region

#Region "Event Handlers"
    ' Insert Method Declarations which Handle an Event Here
#End Region
#End Region


End Class
